<?php
//require_once dirname(dirname(__DIR__))."/common.php";
require_once dirname(__DIR__)."/login_filter.php";

use app\cls\tom\Response;
use app\cls\tom\SysResource;
use app\cls\tom\Tools;
use app\model\HomePage;
$uuid=$Auth['uuid'];
//$uuid=3;
$type_id=$_REQUEST['type_id'];
//var_dump($_POST);exit;
if(empty($type_id)){
    Response::output(-1,"缺少参数");
}

//分页
$page = isset($_REQUEST["page"]) ? intval($_REQUEST["page"]) : 1;
$page = $page > 0 ? $page : 1;
$pageSize = 20;
//实例化数据库
$db = SysResource::getDB(false);
$Home_page = new HomePage($db);
//查询精选数据
$response = $Home_page->type_video($page,$pageSize,$type_id);
//再去根据用户自己的ID去查询用户点赞和关注了哪些视频

//先去redis里查询用户关注和点赞了哪些视频
$redis = SysResource::getRedis();
$uids=sprintf("%d",crc32($uuid));
$key1=REDIS_PREFIX."fabulous_".$uids;
$key2=REDIS_PREFIX."follow_".$uids;
$da1=$redis->HKEYS($key1);//点赞
$da2=$redis->HKEYS($key2);//关注
$zans=[];
$zan_id=[];
$guanzus=[];
if(empty($da1)){
    //数据库查询已点赞视频
    $da1=$Home_page->zan($uuid);
    foreach($da1 as $k=>$v){
        $zans[]=$v['video_uuid'];
        $zan_id[]=$v['id'];
    }

}else{
    $zans=$da1;
}
if(empty($da2)){
    //数据库查询已关注视频
    $da2=$Home_page->guanzu($uuid);
    foreach($da2 as $kk=>$vv){
        $guanzus[]=$vv['other_uuid'];
    }
}else{
    $guanzus=$da2;
}
if(!empty($response['data'])){
    foreach($response['data'] as $kkk=>&$vvv){
        if(in_array($vvv['video_uuid'],$zans) && in_array($vvv['id'],$zan_id)){
            $vvv['is_zan']=2;
        }else{
            $vvv['is_zan']=1;
        }
        if(in_array($vvv['uuid'],$guanzus)){
            $vvv['is_follow']=2;
        }else{
            $vvv['is_follow']=1;
        }
        if($vvv['uuid']==$uuid){
            $vvv['is_user']=1;
        }else{
            $vvv['is_user']=0;
        }
        if(empty($vvv['share_pic'])){
            $vvv['share_pic']="";
        }
        $vvv['count_material_love_10']=round($vvv['count_material_love']*1.7);
        $vvv['count_material_share_10']=round($vvv['count_material_share']*1.7);

        if(empty($vvv['video_desc'])){
            $vvv['video_desc']="";
        }
    }
}

$length=count($response['data']);
if($length<$pageSize){
    Response::output(0,"查询成功",$response['data'],0);
}else{
    Response::output(0,"查询成功",$response['data'],1);
}
